/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.console.command;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.openislands.oi.console.config.RequestWrapper;
import org.openislands.oi.console.config.annotation.Command;
import org.openislands.oi.console.config.annotation.CommandGroup;
import org.openislands.oi.console.config.annotation.CommandParam;
import org.openislands.oi.constant.NodeState;
import org.openislands.oi.pojo.qo.*;
import org.openislands.oi.pojo.vo.CommonResponse;
import org.openislands.oi.pojo.vo.NodeVO;
import org.openislands.oi.util.ConfiguratorUtils;
import org.openislands.oi.util.ObjectUtils;
import org.openislands.oi.apiserver.http.NodeController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;

@SuppressWarnings("unused")
@CommandGroup("node")
public class NodeCommand {
    private static final Logger log = LoggerFactory.getLogger(NodeCommand.class);

    @Command(name = "add", detailMethod = "add", desc = "add union node")
    public RequestWrapper<NodeController, CommonResponse<Boolean>> create(
            String nodeId,
            String nodeName,
            String owner,
            @CommandParam(value = "-pk", required = false, desc = "node tls public key") String publicKey,
            @CommandParam(value = "-desc", required = false) String description,
            String httpAccess,
            @CommandParam(required = false) String httpsAccess,
            @CommandParam(required = false) String grpcAccess,
            @CommandParam(required = false) String grpcsAccess,
            @CommandParam(value = "-tls", required = false, defaultValue = "false") Boolean tlsEnabled) {

        return new RequestWrapper<NodeController, CommonResponse<Boolean>>() {
            @Override
            public CommonResponse<Boolean> request(NodeController target, Map<String, Object> commandParams) {
                NodeAddQO nodeAddQO = ObjectUtils.mapToObject(commandParams, NodeAddQO.class);
                nodeAddQO.setNodeRouteList(Collections.singletonList(ObjectUtils.mapToObject(commandParams, NodeRouteQO.class)));
                return target.addNode(nodeAddQO);
            }

            @Override
            public String resultString(CommonResponse<Boolean> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(detailMethod = "delete", desc = "delete node")
    public RequestWrapper<NodeController, CommonResponse<Boolean>> delete(@CommandParam String nodeId) {
        return new RequestWrapper<NodeController, CommonResponse<Boolean>>() {
            @Override
            public CommonResponse<Boolean> request(NodeController target, Map<String, Object> commandParams) {
                return target.deleteNode(commandParams.get("nodeId").toString());
            }

            @Override
            public String resultString(CommonResponse<Boolean> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(desc = "reset node state info")
    public RequestWrapper<NodeController, CommonResponse<Boolean>> reset(
            String nodeId,
            @CommandParam(required = false, defaultValue = "UNVERIFIED") NodeState state) {
        return new RequestWrapper<NodeController, CommonResponse<Boolean>>() {
            @Override
            public CommonResponse<Boolean> request(NodeController target, Map<String, Object> commandParams) {
                NodeResetQO nodeResetQO = ObjectUtils.mapToObject(commandParams, NodeResetQO.class);
                return target.resetNode(nodeResetQO);
            }

            @Override
            public String resultString(CommonResponse<Boolean> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(detailMethod = "update", desc = "update node info")
    public RequestWrapper<NodeController, CommonResponse<Boolean>> update(
            String nodeId,
            String nodeName,
            String owner,
            @CommandParam(required = false, desc = "node tls public key") String publicKey,
            @CommandParam(value = "-desc", required = false) String description) {
        return new RequestWrapper<NodeController, CommonResponse<Boolean>>() {
            @Override
            public CommonResponse<Boolean> request(NodeController target, Map<String, Object> commandParams) {
                NodeUpdateQO updateQO = ObjectUtils.mapToObject(commandParams, NodeUpdateQO.class);
                return target.updateNode(updateQO);
            }

            @Override
            public String resultString(CommonResponse<Boolean> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(desc = "query myself node info")
    public RequestWrapper<NodeController, CommonResponse<NodeVO>> self() {
        return new RequestWrapper<NodeController, CommonResponse<NodeVO>>() {
            @Override
            public CommonResponse<NodeVO> request(NodeController target, Map<String, Object> commandParams) {
                return target.queryNode("0");
            }

            @Override
            public String resultString(CommonResponse<NodeVO> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(detailMethod = "query", desc = "query one node info")
    public RequestWrapper<NodeController, CommonResponse<NodeVO>> query(@CommandParam(desc = "node id") String nodeId) {
        return new RequestWrapper<NodeController, CommonResponse<NodeVO>>() {
            @Override
            public CommonResponse<NodeVO> request(NodeController target, Map<String, Object> commandParams) {
                return target.queryNode(commandParams.get("nodeId").toString());
            }

            @Override
            public String resultString(CommonResponse<NodeVO> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }

    @Command(detailMethod = "queryPage", desc = "query node info page")
    public RequestWrapper<NodeController, CommonResponse<Page<NodeVO>>> queryPage(
            @CommandParam(required = false, defaultValue = "1") Integer pageNum,
            @CommandParam(required = false, defaultValue = "20") Integer pageSize,
            @CommandParam(required = false) String nodeName,
            @CommandParam(required = false) String owner,
            @CommandParam(required = false) NodeState state,
            @CommandParam(required = false, defaultValue = "true") Boolean orderRule,
            @CommandParam(required = false, defaultValue = "createTime", desc = "options [createTime|updateTime]") String orderField) {
        return new RequestWrapper<NodeController, CommonResponse<Page<NodeVO>>>() {
            @Override
            public CommonResponse<Page<NodeVO>> request(NodeController target, Map<String, Object> commandParams) {
                return target.queryPageNode(ObjectUtils.mapToObject(commandParams, NodePageQO.class));
            }

            @Override
            public String resultString(CommonResponse<Page<NodeVO>> result) {
                return ObjectUtils.object2JsonIndent(result);
            }
        };
    }
}
